package com.example.blogsystem.webAutoTest.common;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class AutoTestUtils {

    private static ChromeDriver chromeDriver;

    /**
     * 创建浏览器驱动对象
     * @return
     */
    public static ChromeDriver getChromeDriver() {
        if (chromeDriver == null) {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            chromeDriver = new ChromeDriver(options);
            // 添加隐式等待
            chromeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        }

        return chromeDriver;
    }

    public static List<String> getTime(){
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        String filename = sim1.format(System.currentTimeMillis());

        SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMdd");
        String dirname = sim2.format(System.currentTimeMillis());
        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }
    public static void getScreenShor(String str) throws IOException {
        List<String> list = getTime();
        String filename = "./src/test/java/com/example/blogsystem/webAutoTest/"+list.get(0)+"/"+str+"_"+list.get(1)+".png";
        File srcfile = chromeDriver.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcfile,new File(filename));
    }
}
